<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDept('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 使用树形组件实现 -->
            <!-- :default-expand-all="true"默认展开 -->
            <el-tree
              class="departments-tree"
              :data="treeData"
              :props="defaultProps"
              :default-expand-all="true"
            >
              <template slot-scope="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  style="height: 50px; width: 100%"
                  align="middle"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addDept(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDept(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDept(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>

        <!-- 添加或编辑弹框 -->
        <el-dialog
          :title="isEdit ? '编辑部门' : '添加部门'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 添加子组件弹层 -->
          <deptDialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @update-dialog="updateDepart"
            @close="closeDialog"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
// 导入子组件
import deptDialog from './deptDialog'

export default {
  components: {
    deptDialog
  },
  data() {
    return {
      treeData: [],
      activeName: 'first', // 被激活的 Tab 标签页
      // 格式化树形空件的
      defaultProps: {
        children: 'children',
        label: 'name'
      },

      showDialog: false, // 控制添加和编辑
      parentId: '', // 父级 ID
      isEdit: false, // 是否是编辑
      originList: []// 传递给子组件的数据
    }
  },
  // 钩子函数
  created() {
    // 调用获取组织架构列表数据方法
    this.getDepartments()
  },
  methods: {
    // 删除部门
    async delDept(id) {
      // 用户点了取消是cancel  确定是confirm
      const confirmRes = await this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmRes === 'cancel') {
        return this.$message('您取消了删除')
      }
      const res = await delDepartment(id)
      // 删除成功需要给用户进行提示
      this.$message.success(res.message)
      // 删除后需要重新获取当前页面数据
      this.getDepartments()
    },

    // 关闭弹窗
    updateDepart() {
      // 关闭弹层
      this.showDialog = false
      // 重新获取数据
      this.getDepartments()
    },
    // 编辑
    editDept(id) {
      this.parentId = id
      this.showDialog = true

      // 将是否是编辑的状态传递给子组件
      this.isEdit = true
    },

    // 添加子部门
    addDept(id) {
      this.parentId = id
      this.showDialog = true
      // 将是否是添加的状态传递给子组件
      this.isEdit = false
    },

    // 获取组织架构列表数据
    async getDepartments() {
      const res = await getDepartments()

      // 格式化传递给子组件的数据
      this.originList = res.data.depts.map(item => ({
        id: item.id,
        code: item.code,
        pid: item.pid,
        name: item.name
      }
      ))

      // 返回的数据进行赋值
      // this.treeData = res.data.depts
      this.treeData = tranListToTreeData(res.data.depts, '')
    },
    // 点击树形控件触发事件
    handleNodeClick(data) {
      console.log(data)
    },

    // 用户点击取消 隐藏弹窗
    closeDialog() {
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }
  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
